package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.ShippingCompanyService;
import com.sz.biz.logistics.base.dto.ShippingCompanyDto;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: ShippingCompanyController <br>
 * Author: wentao.chang <br>
 * Date: 2017-08-04 15:19:00
 */
@RestController
@RequestMapping("${api}/biz/shippingcompanys")
@Api(description = " ", tags = "026-00、航运公司")
public class ShippingCompanyController extends BaseController {
    @Autowired
    private ShippingCompanyService shippingCompanyService;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.biz.shippingcompany}")
    private String i18nTableName;

    @ApiOperation(value = "添加航运公司", notes = "添加航运公司, <br>权限:" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto createShipModel(@RequestBody @ApiParam(value = "航运公司", required = true) ShippingCompanyDto model) {
        Integer id = shippingCompanyService.save(model);
        i18nService.save(id, model.getI18n(), i18nTableName);
        return this.getSaveSuccessDto(MessagesCodes.MODULE_SHIPPINGCOMPANY);
    }

    @ApiOperation(value = "修改航运公司", notes = "修改航运公司<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "航运公司Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "航运公司", required = true, dataType = "ShippingCompanyDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto updateShipModel(@PathVariable("id") int id, @RequestBody ShippingCompanyDto model) {
        shippingCompanyService.update(id, model);
        i18nService.save(id, model.getI18n(), i18nTableName);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_SHIPPINGCOMPANY);
    }

    @ApiOperation(value = "批量删除航运公司", notes = "批量删除航运公司<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto deleteShipModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        shippingCompanyService.deleteByIds(ids);
        return this.getDeleteSuccessDto(MessagesCodes.MODULE_SHIPPINGCOMPANY);
    }

    @ApiOperation(value = "获取航运公司", notes = "获取航运公司<br>权限：")
    @ApiImplicitParam(name = "id", value = "航运公司Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getShipModel(@PathVariable("id") int id) {
        ShippingCompanyDto dto = shippingCompanyService.findById(id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "检查航运公司名是否存在", notes = "检查航运公司名是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "nameen", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto checNameENExist(@RequestParam(required = false) String name,
                                    @RequestParam(required = false) Integer id) {
        shippingCompanyService.checkShippingCompanyName(name, id);
        return new ResultDto();
    }

    @ApiOperation(value = "检查航运公司编码是否存在", notes = "检查航运公司公司编码是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto checkCodeExist(@RequestParam(required = false) String code,
                                    @RequestParam(required = false) Integer id) {
        shippingCompanyService.checkShippingCompanyCode(code, id);
        return new ResultDto();
    }

    @ApiOperation(value = "获取航运公司", notes = "获取航运公司<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryId", value = "国家Id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(required = false) String countryId,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "20") int pageSize,
                                   @RequestParam(required = false) String sort,
                                   @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingCompanyService.findShippingCompanyListPage(new QueryParams(pageSize, pageIndex, q, sort, asc).put("countryId", countryId));
    }

    @ApiOperation(value = "获取航运公司短接口", notes = "获取航运公司短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto getShipModels(@RequestParam(required = false) String q,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "20") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingCompanyService.findShippingCompanyShortListPage(new QueryParams(pageSize, pageIndex, q, null, true));
    }

    @ApiOperation(value = "获取航运公司(i18n)", notes = "获取航运公司<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "language", value = "语言代码", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "unfilled", value = "是否未添加国际化字段，true，代表未添加，fasle与null代表所有",
                    dataType = "boolean", paramType = "query", defaultValue = "false"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/i18n/{language}", method = {RequestMethod.GET})
    public ResultDto getShipModelsWithI18n(@PathVariable String language,
                                           @RequestParam(required = false) String q,
                                           @RequestParam(defaultValue = "1") int pageIndex,
                                           @RequestParam(defaultValue = "20") int pageSize,
                                           @RequestParam(defaultValue = "false") Boolean unfilled,
                                           @RequestParam(required = false) String sort,
                                           @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return shippingCompanyService.findI18nByParam(new QueryParams(pageSize, pageIndex, q, sort, asc, language).put("unfilled", unfilled));
    }

    @ApiOperation(value = "保存航运公司国际化(i18n)", notes = "保存航运公司国际化信息<br>权限：" + Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    @RequestMapping(value = "/i18n", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_CARRIER_MARITIME)
    public ResultDto setShipModelI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true)
                                              List<I18nDto> models) {
        i18nService.save(models, i18nTableName);
        shippingCompanyService.refreshCache();
        return this.getSaveI18nSuccessDto();
    }
}
